package com.honey.eclipse.formatter;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.jdt.core.ToolFactory;
import org.eclipse.jdt.core.formatter.CodeFormatter;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.text.edits.TextEdit;
import org.java.plugin.Plugin;

import com.honey.core.Extension;
import com.honey.core.compiler.CompilationType;
import com.honey.core.formater.CodeFormater;
import com.honey.eclipse.formatter.style.BaseStyleFormatterSetting;
import com.honey.eclipse.formatter.style.DefaultStyle;

public class EclipseFormatterExtension extends Extension<Plugin> implements CodeFormater{
	
	private EclipseFormatterPlugin eclipseFormatterPlugin = null;
	
	private static final Map<String, CodeFormatter> cacheCodeFormatter= new HashMap<String, CodeFormatter>();
	
	public EclipseFormatterExtension(Plugin belongPlugin) {
		super(belongPlugin);
		eclipseFormatterPlugin =(EclipseFormatterPlugin)belongPlugin;
	}

	@Override
	public void clean() {
		
	}

	@Override
	public void format(File code,String charset) {
		Map<String, String> setting = null;
		String cacheKey = "default";
		if(eclipseFormatterPlugin.getStyle() == Style.DEFAULT ){
			setting = ( new DefaultStyle()).getSettings();
		}else{
			setting = (new BaseStyleFormatterSetting(eclipseFormatterPlugin.getStyleStettingFile())).getSettings();
			cacheKey = eclipseFormatterPlugin.getStyleStettingFile();
		}
		CodeFormatter codeFormatter = cacheCodeFormatter.get(cacheKey);;
		if( codeFormatter == null){
			codeFormatter = ToolFactory.createCodeFormatter(setting);
			cacheCodeFormatter.put(cacheKey, codeFormatter);
		}
		
		BufferedReader in = null;
		BufferedWriter out = null;
		String line;
		String lineSeperator = System.getProperty("line.separator");
		try {
			in = new BufferedReader(new InputStreamReader(new FileInputStream( code ),charset));
			StringBuffer sb = new StringBuffer();
			while ((line = in.readLine()) != null) {
				sb.append(line);
				sb.append(lineSeperator);
			}
			in.close();
			in = null;
			String contents = sb.toString();
			
			IDocument doc = new Document(contents);
			TextEdit edit = codeFormatter.format(
					CodeFormatter.K_COMPILATION_UNIT,
					contents,
					0,
					contents.length(),
					0,
					lineSeperator);
			edit.apply(doc);
			out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(code), charset));
			String output = doc.get();
			out.write(output);
			out.flush();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.close();
				}
			} catch (Exception e) {

			}
		}
	}
	
	@Override
	public CompilationType[] supportCompilationTypes() {
		return new CompilationType[]{
			CompilationType.JAVA //支持java文件格式化
		};
	}
}
